<template>
    <div class="user">
        <Breadcrumb></Breadcrumb>
        <el-card class="box-card">
            <el-row :gutter="20">
                <el-col :span="10">
                    <el-input placeholder="请输入内容" class="input-with-select" v-model="query.query" clearable @clear="getTableData()">
                        <el-button slot="append" icon="el-icon-search" @click="getTableData()" ></el-button>
                    </el-input>
                </el-col>
                <el-button type="primary" @click="addFormVisible = true">添加</el-button>
            </el-row>
            <el-table :data="tableData" style="width: 100%">
                <el-table-column fixed type="index" label="#">
                </el-table-column>
                <el-table-column prop="username" label="姓名">
                </el-table-column>
                <el-table-column prop="email" label="邮箱">
                </el-table-column>
                <el-table-column prop="mobile" label="电话">
                </el-table-column>
                <el-table-column prop="address" label="状态">
                    <template slot-scope="scope">
                        <el-switch active-color="#13ce66" inactive-color="#ff4949" @change="updateSwitch(scope.row)"
                            v-model="scope.row.mg_state">
                        </el-switch>
                    </template>
                </el-table-column>
                <el-table-column prop="zip" label="操作">
                    <template slot-scope="scope">
                        <el-button type="primary" icon="el-icon-edit" circle size="mini" @click="edit(scope.row.id)"></el-button>
                        <el-button type="danger" icon="el-icon-delete" circle size="mini" @click="delObj(scope.row.id)">
                        </el-button>
                        <el-button type="warning" icon="el-icon-star-off" circle size="mini"></el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>

        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page="query.pagenum" :page-sizes="[2, 3, 5, 6]" :page-size="query.pagesize"
            layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>



        <el-dialog title="添加用户" :visible.sync="addFormVisible">
            <el-form :model="addForm" :rules="rulesForm" ref="RefForm">
                <el-form-item label="用户名" :label-width="formLabelWidth" prop="username">
                    <el-input v-model="addForm.username" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="密码" :label-width="formLabelWidth" prop="password">
                    <el-input v-model="addForm.password" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" :label-width="formLabelWidth" prop="email">
                    <el-input v-model="addForm.email" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="手机" :label-width="formLabelWidth" prop="mobile">
                    <el-input v-model="addForm.mobile" autocomplete="off"></el-input>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="addFrom">确 定</el-button>
            </div>
        </el-dialog>

        <el-dialog title="编辑用户" :visible.sync="editFormVisible">
            <el-form :model="editForm" :rules="rulesForm" ref="RefForm">
                <el-form-item label="用户名" :label-width="formLabelWidth" prop="username">
                    <el-input v-model="editForm.username" autocomplete="off" disabled></el-input>
                </el-form-item>
                <el-form-item label="邮箱" :label-width="formLabelWidth" prop="email">
                    <el-input v-model="editForm.email" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="手机" :label-width="formLabelWidth" prop="mobile">
                    <el-input v-model="editForm.mobile" autocomplete="off"></el-input>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="editFrom">确 定</el-button>
            </div>
        </el-dialog>


    </div>
</template>

<script>
import Breadcrumb from '@/views/Breadcrumb.vue'
import { getUser, UpdateSwitch, delUsers, addUsers ,editUsers,editFromUsers} from '@/utils/User'
export default {
    components: { Breadcrumb },

    data() {
        var checkAge = (rule, value, callback) => {
            let reg = /^1[3-9]\d{9}$/
            if (reg.test(value)) {
                callback()
            } else {
                callback('验证不成功')
            }
        }
        return {

            tableData: [],
            query: {
                query: '',
                pagenum: 1,
                pagesize: 5
            },
            total: 0,
            addFormVisible: false,
            editFormVisible:false,
            formLabelWidth: '120px',
            addForm: {
                username: '',
                password: '',
                email: '',
                mobile: ''
            },
            rulesForm: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' }
                ],
                email: [
                    { required: true, message: '请输入邮箱', trigger: 'blur' },
                    { type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
                ],
                mobile: [
                    { required: true, message: '请输入电话号码', trigger: 'blur' },
                    { validator: checkAge, message: '请输入正确的电话号码格式', trigger: 'blur' }
                ],
            },
            editForm:{
                username: '',
                email: '',
                mobile: ''
            }
        }
    },
    methods: {
        async getTableData() {
            let { data: res } = await getUser(this.query)
            this.tableData = res.data.users
            this.total = res.data.total
        },
        handleSizeChange(val) {
            this.query.pagesize = val
            this.getTableData()
        },
        handleCurrentChange(val) {
            this.query.pagenum = val
            this.getTableData()
        },
        async updateSwitch(obj) {
            let { data: res } = await UpdateSwitch(obj)
            if (res.meta.status == 200) {
                this.$message.success(res.meta.msg)
                this.getTableData()
            } else {
                this.$message.error(res.meta.msg)
            }
        },
        delObj(id) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                let { data: res } = await delUsers(id)
                if (res.meta.status == 200) {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getTableData()
                }
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        addFrom() {
            this.$refs.RefForm.validate(async (valid) => {
                if (valid) {
                    let { data: res } = await addUsers(this.addForm)
                    if (res.meta.status == 201) {
                        this.$message.success(res.meta.msg)
                        this.addFormVisible = false
                        this.getTableData()
                        this.addForm = {
                            username: '',
                            password: '',
                            email: '',
                            mobile: ''
                        }
                    } else {
                        this.$message.error(res.meta.msg)
                    }
                }
            });
        },
        async edit(id){
            this.editFormVisible = true
            let {data:res} = await editUsers(id)
            this.editForm = res.data
        },
        editFrom(){
            this.$refs.RefForm.validate(async (valid) => {
                if (valid) {
                    let { data: res } = await editFromUsers(this.editForm.id,this.editForm)
                    if (res.meta.status == 200) {
                        this.$message.success(res.meta.msg)
                        this.editFormVisible = false
                        this.getTableData()
                       
                    } else {
                        this.$message.error(res.meta.msg)
                    }
                }
            });
        }
    },
    mounted() {
        this.getTableData()
    }

}
</script>

<style lang="scss">

</style>